#include <Arduino.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
#include "demos\lv_demos.h"

#include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
#include <Ticker.h>

float pi = 3.14;
float Te ;
float Hu ;
char num[10]  ;
char temp[10] ;
char humi[10] ;

DHTesp dht;
void getTemperature();
void triggerGetTemp();

Ticker tempTicker;
int dhtPin = 12;

unsigned long lastResetTime = 0;  
const unsigned long RESET_INTERVAL = 1000; // 修改lvgl重置时间 单位毫秒 

void initTemp(){
dht.setup(dhtPin, DHTesp::DHT11);
}

void getTemperature()
{
    // tempTicker.attach(10, triggerGetTemp);
    TempAndHumidity newValues = dht.getTempAndHumidity();
    Serial.println(" T:" + String(newValues.temperature) + " H:" + String(newValues.humidity));
      Te = (newValues.temperature);
      Hu = (newValues.humidity);
      delay(5);

}


/*Change to your screen resolution*/
static const uint16_t screenWidth = 160;
static const uint16_t screenHeight = 128;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * screenHeight];

TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */

/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
  uint32_t w = (area->x2 - area->x1 + 1);
  uint32_t h = (area->y2 - area->y1 + 1);

  tft.startWrite();
  tft.setAddrWindow(area->x1, area->y1, w, h);
  tft.pushColors((uint16_t *)&color_p->full, w * h, true);
  tft.endWrite();

  lv_disp_flush_ready(disp);
}

void lvgl_init_01()
{
  tft.begin();        /* TFT init */
  tft.setRotation(3); /* Landscape orientation, flipped */

  lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * screenHeight);

  /*Initialize the display*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  /*Change the following line to your display resolution*/
  disp_drv.hor_res = screenWidth;
  disp_drv.ver_res = screenHeight;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &draw_buf;
  lv_disp_drv_register(&disp_drv);

  // /*在这插入事件函数*/
  // lv_obj_t *label1 = lv_label_create(lv_scr_act());
  // lv_label_set_text(label1, "Tem=");
  // lv_obj_align(label1, LV_ALIGN_OUT_LEFT_TOP, 20, 40);

  // lv_obj_t *label2 = lv_label_create(lv_scr_act());
  // lv_label_set_text(label2, temp);
  // lv_obj_align(label2, LV_ALIGN_OUT_LEFT_TOP, 70, 40);
  // lv_obj_del(label2);
  // lv_obj_t *label3 = lv_label_create(lv_scr_act());
  // lv_label_set_text(label3, "Hum=");
  // lv_obj_align(label3, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 60);

  // lv_obj_t *label4 = lv_label_create(lv_scr_act());
  // lv_label_set_text(label4, humi);
  // lv_obj_align(label4, LV_ALIGN_OUT_BOTTOM_LEFT, 70, 60);

  // lv_obj_t *label5 = lv_label_create(lv_scr_act());
  // lv_label_set_text(label5, "Test=");
  // lv_obj_align(label5, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 80);

  // lv_obj_t *label6 = lv_label_create(lv_scr_act());
  // lv_label_set_text(label6, num);
  // lv_obj_align(label6, LV_ALIGN_OUT_BOTTOM_LEFT, 60, 80);
}

void lvgl_loop_sc()
{
	

  lv_obj_t *label1 = lv_label_create(lv_scr_act());
  lv_label_set_text(label1, "Tem=");
  lv_obj_align(label1, LV_ALIGN_OUT_LEFT_TOP, 20, 40);

  lv_obj_t *label2 = lv_label_create(lv_scr_act());
  lv_label_set_text(label2, temp);
  lv_obj_align(label2, LV_ALIGN_OUT_LEFT_TOP, 70, 40);
  
  lv_obj_t *label3 = lv_label_create(lv_scr_act());
  lv_label_set_text(label3, "Hum=");
  lv_obj_align(label3, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 60);

  lv_obj_t *label4 = lv_label_create(lv_scr_act());
  lv_label_set_text(label4, "");
  lv_label_set_text(label4, humi);
  lv_obj_align(label4, LV_ALIGN_OUT_BOTTOM_LEFT, 70, 60);

  lv_obj_t *label5 = lv_label_create(lv_scr_act());
  lv_label_set_text(label5, "Test=");
  lv_obj_align(label5, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 80);

  lv_obj_t *label6 = lv_label_create(lv_scr_act());
  lv_label_set_text(label6, num);
  lv_obj_align(label6, LV_ALIGN_OUT_BOTTOM_LEFT, 60, 80);
	
	
}

void lvgl_loop_cl()
{
  lv_obj_t *label1 = lv_label_create(lv_scr_act());
  lv_label_set_text(label1, "");
  lv_obj_align(label1, LV_ALIGN_OUT_LEFT_TOP, 20, 40);

  lv_obj_t *label2 = lv_label_create(lv_scr_act());
  lv_label_set_text(label2, "");
  lv_obj_align(label2, LV_ALIGN_OUT_LEFT_TOP, 70, 40);
  
  lv_obj_t *label3 = lv_label_create(lv_scr_act());
  lv_label_set_text(label3, "");
  lv_obj_align(label3, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 60);

  lv_obj_t *label4 = lv_label_create(lv_scr_act());
  lv_label_set_text(label4, "");
  lv_obj_align(label4, LV_ALIGN_OUT_BOTTOM_LEFT, 70, 60);

  lv_obj_t *label5 = lv_label_create(lv_scr_act());
  lv_label_set_text(label5, "");
  lv_obj_align(label5, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 80);

  lv_obj_t *label6 = lv_label_create(lv_scr_act());
  lv_label_set_text(label6, "");
  lv_obj_align(label6, LV_ALIGN_OUT_BOTTOM_LEFT, 60, 80);
	
}

void setup()
{
  initTemp();
  lv_init();
  lvgl_init_01();
  Serial.begin(9600);
  
}

void loop()
{   
  unsigned long currentTime = millis();  
    if (currentTime - lastResetTime >= RESET_INTERVAL) {  
        lastResetTime = currentTime; // 更新时间，以便下次比较  
        lv_timer_handler();             // 重置GUI  by lvgl
    }  

  getTemperature();               // 获取数值

  dtostrf(pi, 3, 2, num); //6     // 传递数值
	dtostrf(Hu, 3, 2, humi);//4
	dtostrf(Te, 3, 2, temp);//2


	lvgl_loop_sc();                 // 显示数值   by lvgl
  // lvgl_loop_cl();
    delay(5);

}

